package com.tfblog.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tfblog.blog.entity.Comment;
import com.tfblog.blog.vo.CommentVO;
import com.tfblog.blog.mapper.CommentMapper;
import com.tfblog.blog.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tfeng
 * @since 2021-04-26
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    @Autowired
    private CommentMapper commentMapper;

    /*
    * 如果是顶级评论,则 parentCommentId = null (使用isNULL进行条件判断)
    * */
    @Override
    public List<CommentVO> listCommentByBlogId(Long blogId) {
        //首先我们按照博客id找到所有的一级评论,即根评论集合
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",blogId).isNull("parent_comment_id").orderByDesc("create_time");
        List<Comment> comments = commentMapper.selectList(wrapper);
        //我们实现,将二级,三级评论统一按照顺序放置在二级评论中,避免评论一直扩展...
        return combineChildren(comments);
    }

    /*
    * commentsView : 顶级的评论节点
    * */
    private List<CommentVO> combineChildren(List<Comment> commentsView) {
        //定义一个list集合: 保存每一个顶级评论,以及对应的子集评论集合
        List<CommentVO> list = new ArrayList<>();
        for (Comment comment : commentsView) {
            //获取该顶级评论的回复子评论
            List<Comment> childrenComments =
            commentMapper.selectList(new QueryWrapper<Comment>().eq("parent_comment_id",comment.getId()));
            for (Comment reply : childrenComments) {
                //循环迭代,找到子代,存放到tempReplys集合中
                recursively(reply);
            }
            //我们创建commentVO视图对象
            CommentVO commentVO = new CommentVO();
            //将顶层评论的属性拷贝到commentVO对象中
            BeanUtils.copyProperties(comment,commentVO);
            //同时将该顶层评论下的一级,二级,...n级子评论统一保存在tempReplys集合中,然后保存在该顶层commentVO对象的Replycomments属性中
            commentVO.setReplycomments(tempReplys);
            list.add(commentVO);
            //清除临时存放区[记得设置为空...否则会叠加...]
            tempReplys = new ArrayList<>();
        }
        //最后返回视图对象CommentVO的集合
        return list;
    }

    /*
    * 存放被迭代的对象(使用递归迭代的方法层层遍历...)
    * */
    private void recursively(Comment comment) {
        tempReplys.add(comment); //将顶级节点的一级节点添加到tempReplys临时集合中
        //判断一级节点是否存在子评论
        List<Comment> childrenComments =
                commentMapper.selectList(new QueryWrapper<Comment>().eq("parent_comment_id",comment.getId()));
        //如果不为空
        if (childrenComments.size() > 0) {
            //说明存在子评论,我们对于子评论,进行递归遍历
            for (Comment reply : childrenComments) {
                recursively(reply);
            }
        }
    }


    /*
    * 获取对应父级评论的评论信息内容
    * */
    @Override
    public Comment findParentComment(Long parentCommentId) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("id",parentCommentId);
        Comment comment = commentMapper.selectOne(wrapper);
        return comment;
    }
}
